package tools;

import book.Book;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA
 * Description:二分查找
 * User:22954
 * Date:2024-01-13
 * Time:19:24
 */
public class BinarySearch {
    public int binarytpSearch(ArrayList<Book> arr, String name) {
        int right = arr.size() - 1;
        int left = 0;
        NameComparator nameComparator = new NameComparator();
        while (left <= right) {
            int mid = (right + left) >>> 1;
            int x = nameComparator.compare(arr.get(mid).getName(), name);
            if (x == 0) {
                return mid;
            } else if (x > 0) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
